<template>
  <div class="box">
    <div class="title">
      <p>
        处方状态：
        <span>{{status | statusFilter}} {{status == 5 ? `( 原因：${recipeDownCause} )` : ''}}</span>
      </p>
      <div class="cancelBox cursor" v-if="status == 1" @click="deleteRecipe">取消处方</div>
      <div v-if="whetherOffRedo == '0'">
        <div
          class="cancelBox cursor"
          v-if="status == 3 || status == 5"
          style="background:#FF6532;"
          @click="dialogTableVisible = true"
        >重新开处方</div>
      </div>
    </div>
    <div class="modelInfo">
      <vue-scroll ref="vs" :ops="$store.state.ops">
        <div class="modelInfo_box">
          <div class="prescription_content_box">
            <img :src="recipeImageUrl" alt />
          </div>
        </div>
      </vue-scroll>
    </div>
    <!-- <medicalHistoryDialog :showTrue="dialogTableVisible" :patientData="patientData" @close="dialogClose" @submit="submitPrescription"></medicalHistoryDialog> -->
    <medicalHistoryDialog :showTrue="dialogTableVisible" :patientData="patientData" @close="dialogClose" @submit="savePrescription"></medicalHistoryDialog>
    <submitPrescriptiony
      :showTrue="dialogShowSubmit"
      :prescriptionData="prescriptionData"
      :reviewShow="submitBtn"
      @close="closeModel"
      @change="changePrescription"
      @submit="submitPrescription"
    ></submitPrescriptiony>
  </div>
</template>
<script>
import medicalHistoryDialog from "../../components/dialog/medicalHistoryDialog";
import submitPrescriptiony from "../../components/dialog/depengingDialog";
import {
  getRecipeControllerRecips,
  getRecipeControllerAddRecip,
  getRecipeControllerUpdateStatus,
  getRecipeControllerUpdateRecipe
} from "../../requestApi/prescription.js";

export default {
  components: {medicalHistoryDialog,submitPrescriptiony},
  data() {
    return {
      dialogTableVisible: false,
      dialogShowSubmit: false,
      submitBtn: true, // 显示处方弹出框底部按钮
      recipeOddNumbers: "", //处方单号
      prescriptionData: {}, // 处方信息
      userInfo: {},
      doctorNameImage: "",
      checkDoctorNameImage: "",
      status: 99,
      recipeDownCause: "",
      recipeImageUrl: "",
      recipeId: "",
      orderId: "",
      whetherOffRedo: "",
      patientData:'',
      closeMedicalDialog: false// 是否关闭添加病例处方
    };
  },
  created: function() {
    this.recipeId = this.$route.query.recipeId;
    this.listFun();
  },
  mounted() {},
  methods: {
    //关闭重新开处方弹出框方法
    dialogClose(val) {
      this.dialogTableVisible = val;
    },
    //关闭重新开处方弹出框方法
    closeModel(val) {
      this.dialogShowSubmit = val;
      if(this.closeMedicalDialog){
        this.dialogTableVisible = false;
      }
    },
    // 完成处方填写---打开提交页
    savePrescription(val){
      let self = this
      var userInfo = {
        orderId: this.patientData.orderId,
        patientId: this.patientData.patientId,
        userId: this.patientData.userId,
        createTime: this.patientData.createTime,
        name: this.patientData.patientName,
        sex: this.patientData.patientSex,
        age: this.patientData.patientAge,
        recipeType: 0, // 处方类型: 0普通处方, 1其他
        recipeSource: 0, // 处方来源:0=web端,1=一体机,
        doctorId: this.patientData.doctorId,
        doctorName: this.patientData.doctorName,
        drugstoreId: this.patientData.drugstoreId, //购药门店id
        illnessDesc: val.illnessDesc, // 病情描述
        firstDiagnose: val.firstDiagnose, // 初步诊断
        treatmentSuggestion: val.treatmentSuggestion //处理意见
      };
      this.prescriptionData = { drugList: val.drugList, userInfo: userInfo };
      this.closeMedicalDialog = true;
      this.dialogShowSubmit = true;
    },
    // 重新编辑处方病历---打开填写页
    changePrescription() {
      this.closeMedicalDialog = false;
      this.dialogShowSubmit = false;
    },
    // 提交处方
    async submitPrescription(val) {
      let doctorInfo = {
        userId: this.userInfo.userId,
        patientId: this.userInfo.patientId,
        name: this.userInfo.name,
        sex: this.userInfo.sex,
        age: this.userInfo.age,
        firstDiagnose: this.userInfo.firstDiagnose,
        id: this.recipeId,
        drugList:this.prescriptionData.drugList,
        recipeType: 0,
        recipeSource: 0,
        orderId: this.orderId,
        doctorId: localStorage.getItem("doctorId"),
        doctorName: localStorage.getItem("doctorName"),
        drugstoreId: this.userInfo.drugstoreId //购药门店id
      };
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let data = await getRecipeControllerAddRecip(doctorInfo);
      loading.close()
      if (data.code == 200) {
        this.recipeId = data.data;
        this.closeMedicalDialog = true;
        this.dialogTableVisible = false;
        this.dialogShowSubmit = false;
        this.listFun();
      }
      else{
        this.closeMedicalDialog = true;
        this.dialogTableVisible = false;
        this.dialogShowSubmit = false;
        this.$message({
          type: "error",
          message: data.message
        });
      }
    },
    //取消处方
    async deleteRecipe() {
      let data = await getRecipeControllerUpdateStatus({
        recipeId: this.recipeId
      });
      if (data.code == 200) {
        this.status = 3;
        this.$message({
          type: "info",
          message: "取消处方成功"
        });
      }
    },
    //初始化数据
    async listFun() {
      let data = await getRecipeControllerRecips({
        recipeId: this.recipeId
      });
      if (data.code == 200) {
        if (data.data) {
          let {
            drugList,
            recipeOddNumbers,
            recipeType,
            doctorName,
            doctorNameImage,
            checkDoctorNameImage,
            status, //  处方状态:1待审核, 2已通过, 3已取消, 4已失效, 5不通过
            recipeImageUrl,
            orderId,
            whetherOffRedo,
            recipeDownCause,
            ...userInfo
          } = data.data;
          this.userInfo = userInfo;
          this.recipeOddNumbers = recipeOddNumbers;
          this.doctorNameImage = doctorNameImage;
          this.checkDoctorNameImage = checkDoctorNameImage;
          this.status = status;
          this.recipeImageUrl = recipeImageUrl;
          this.orderId = orderId;
          this.whetherOffRedo = whetherOffRedo;
          this.recipeDownCause = recipeDownCause;
          this.patientData = {
            orderId: data.data.orderId,
            patientId: data.data.patientId,
            userId: data.data.userId,
            patientName: data.data.name,
            patientSex: data.data.sex,
            patientAge: data.data.age,
            createTime: data.data.createDate,
            doctorId: data.data.doctorId || localStorage.getItem("doctorId"),
            doctorName: data.data.doctorName || localStorage.getItem("doctorName"),
            drugstoreId: data.data.drugstoreId,
            illnessDesc: data.data.illnessDesc, // 病情描述
            firstDiagnose: data.data.firstDiagnose, // 初步诊断
            treatmentSuggestion: data.data.treatmentSuggestion, //处理意见
            drugList: data.data.drugList,
            showMedicalRecord: false
          }
        }
      }
    }
  },
  filters: {
    timeFilter(val) {
      if (val) {
        var standardTime = new Date(val * 1);
        var y, m, d, h, f;
        y = standardTime.getFullYear();
        m = standardTime.getMonth() + 1;
        m = m > 9 ? m : "0" + m;
        d = standardTime.getDate();
        d = d > 9 ? d : "0" + d;
        h = standardTime.getHours();
        h = h > 9 ? h : "0" + h;
        f = standardTime.getMinutes();
        f = f > 9 ? f : "0" + f;
        return `${y}.${m}.${d} ${h}:${f}`;
      } else {
        return "暂无";
      }
    },
    sexFilter(val) {
      if (val == "0") {
        return "男";
      } else {
        return "女";
      }
    },
    statusFilter(val) {
      if (val == "1") {
        return "待审核";
      } else if (val == "2") {
        return "已通过";
      } else if (val == "3") {
        return "已取消";
      } else if (val == "4") {
        return "已失效";
      } else if (val == "5") {
        return "不通过";
      }
    }
  },
  watch: { 
    $route(to,from){
      this.recipeId = this.$route.query.recipeId;
      this.listFun()
    },
  },
  computed: {}
};
</script>
<style scoped>
.box {
  width: 100%;
  background-color: #fff;
  overflow: hidden;
  height: 100%;
}
.title {
  height: 60px;
  line-height: 60px;
  padding-left: 40px;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.1);
  border-radius: 4px 4px 0px 0px;
  position: relative;
  display: flex;
  align-items: center;
}
.title > p {
  font-size: 14px;
  font-family: MicrosoftYaHei;
  color: rgba(42, 41, 40, 1);
  font-weight: bolder;
}
.title > p > span {
  color: rgba(255, 60, 60, 1);
}
.modelInfo {
  height: calc(100% - 80px);
  overflow: hidden;
}
.modelInfo_box {
  overflow: hidden;
  height: 100%;
  padding-bottom: 20px;
}
.prescription_content_box {
  width: 720px;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.05);
  border-radius: 6px;
  border: 1px solid rgba(246, 246, 246, 1);
  margin: 0 auto;
  box-sizing: border-box;
  padding: 40px;
  margin-bottom: 50px;
  margin-top: 30px;
}
.prescription_content_box > img {
  width: 100%;
  height: 100%;
}
.content_title {
  text-align: center;
  font-size: 20px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: rgba(42, 41, 40, 1);
  margin-bottom: 30px;
}
.title_img {
  width: 46px;
  height: 46px;
  position: absolute;
  top: 20px;
  right: 40px;
}
.brClass {
  height: 1px;
  background: rgba(0, 0, 0, 1);
  margin-bottom: 1px;
}
.prescription_number {
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: rgba(42, 41, 40, 1);
  margin-top: 20px;
}
.prescription_content {
  box-sizing: border-box;
  margin-top: 15px;
  padding: 24px 20px;
  border-radius: 3px;
  border: 1px solid rgba(228, 228, 228, 1);
}
.userName_label {
  display: inline-block;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(122, 118, 118, 1);
  margin-right: 12px;
}
.userName_content {
  display: inline-block;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(55, 55, 55, 1);
  margin-right: 100px;
}
.diagnosis {
  margin-top: 12px;
}
.drugsList {
  margin-top: 20px;
  width: 640px;
  border-radius: 3px;
  border: 1px solid rgba(228, 228, 228, 1);
  box-sizing: border-box;
  padding: 20px;
}
.drugsImg {
  display: block;
  width: 24px;
  height: 24px;
}
.drugsItem {
  margin-top: 14px;
}
.itemTitle {
  font-size: 14px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: bold;
  color: rgba(55, 55, 55, 1);
}
.itemContent {
  box-sizing: border-box;
  padding-left: 15px;
  margin-top: 8px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(122, 118, 118, 1);
}
.itemContent > span {
  display: inline-block;
  margin-right: 28px;
}
.cancelBox {
  float: right;
  width: 120px;
  height: 34px;
  background: rgba(42, 41, 40, 1);
  border-radius: 20px;
  position: absolute;
  top: 13px;
  right: 40px;
  color: #fff;
  cursor: pointer;
  line-height: 34px;
  text-align: center;
}
.alertBox {
  position: fixed;
  width: 100vw;
  height: 100vh;
  z-index: 9;
}
.dialogTitle {
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: rgba(42, 41, 40, 1);
}
.dialogTitle > span {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(55, 55, 55, 1);
}
.footer {
  width: 640px;
  height: 121px;
  border-radius: 3px;
  border: 1px solid rgba(228, 228, 228, 1);
  margin-top: 20px;
  margin-bottom: 30px;
  box-sizing: border-box;
  padding: 20px;
  overflow: hidden;
}
.imgBox {
  float: left;
  width: 280px;
  height: 80px;
}
.imgBox > span {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(122, 118, 118, 1);
}
.imgBox > img {
  vertical-align: top;
  margin-left: 12px;
  width: 150px;
  height: 70px;
}
</style>